const app = getApp();

Page({
  data: {
    id:'',
    reason_id:'',
    orderInfo: {},
    returnInfo:'请选择退货原因',
    returnList:[],
    arrIndex:0
  },
  onShareAppMessage() {
    return {
      title: '花礼鲜花玫瑰生日',
      desc: '花礼鲜花玫瑰生日',
      path: 'pages/todos/todos'
    };
  },
  onLoad(r){
    var that = this;
    my.setNavigationBar({
      title: '申请退货',
    });
    my.httpRequest({
      url: app.geturl+'/refund-reason/list',
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        console.info(res);
        var arrObj = Array();
        for(var i=0;i<res.data.data.list.length;i++){
          var arr = new Object();
          arr['name'] = res.data.data.list[i].reason_info;
          arr['id'] = res.data.data.list[i].reason_id;
          arrObj[i] = arr;
        }
        console.info(arrObj);
        that.setData({
          returnList:arrObj,
          reason_id:arrObj[0].id
        })
      }
    });
    my.getStorage({
      key: 'token',
      success: function(res) {
        my.httpRequest({
          url: app.geturl+'/order/info',
          method: 'GET',
          data: {
            token: res.data.token,
            order_id:r.id
          },
          dataType: 'json',
          success: function(res) {
            if(res.data.code==0){
              console.info(res.data.data.Obj);
              var goods_id = '';
              for(var i=0;i<res.data.data.Obj.goods.length;i++){
                goods_id+= res.data.data.Obj.goods[i].order_goods_id+',';
              }
              var goods_id = goods_id.substr(0,goods_id.length-1);
              console.info(goods_id);
              that.setData({
                orderInfo: res.data.data.Obj,
                id:r.id,
                goods_id:goods_id
              })
            }else{
              app.memberLogin();
            }
          }
        });
      }
    });
  },
  subReturn(){
    var that = this;
    console.info(that.data);
    my.getStorage({
      key: 'token',
      success: function(res) {
        my.httpRequest({
          url: app.geturl+'/refund-return/apply?token='+res.data.token,
          method: 'POST',
          data: {
            reason_id:that.data.reason_id,
            refund_type:2,
            order_goods_id:that.data.goods_id
          },
          dataType: 'json',
          success: function(res) {
            if(res.data.code==0){
              console.info(res);
              my.navigateBack({
                delta: 1
              })
            }else{
              app.memberLogin();
            }
          }
        });
      }
    });
  },
  bindObjPickerChange(e) {
    var info = this.data.returnList;
    this.setData({
      reason_id: info[e.detail.value].id,
    });
  },
  onBlur(e) {
    this.setData({
      inputValue: e.detail.value,
    });
  },
  add() {
    app.todos = app.todos.concat([{
      text: this.data.inputValue,
      compeleted: false,
    }]);

    my.navigateBack();
  },
  handleTap(){
    my.navigateTo({ url: '../todos/todos' });
  }
});
